<?php
/*
	消息订阅设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if(!$_SESSION['plugin_id']){
	exi('请先选择插件！','error');
}
$tabs=array(
	array('op'=>'list','title'=>'订阅事件列表'),
	array('op'=>'edit','title'=>'添加事件订阅'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('plugin_id'=>$_SESSION['plugin_id']);
	$list=pdo_getall('core_plugin_subscribe',array('subscriber'=>$_SESSION['plugin_name']));
	
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'list'=>array('pagelist'=>false,'tip'=>'','data'=>'list','list'=>array(
			array('name'=>'plugin','title'=>'订阅插件标识','type'=>'string'),
			array('name'=>'event','title'=>'订阅事件标识','type'=>'string'),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_plugin_subscribe',array('id'=>$id));
	}
	$return['item']=$item?:array();
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'edits'=>array(
			array(
				'title'=>'权限设置',
				'list'=>array(
					array('type'=>'string','title'=>'插件标识','name'=>'plugin','help'=>'如果是订阅系统事件，此处填写core'),
					array('type'=>'string','title'=>'事件标识','name'=>'event'),
				),
			)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'plugin'=>trim($post['plugin']),
		'event'=>trim($post['event']),
	);
	$id=intval($post['id']);
	if($id){
		pdo_update('core_plugin_subscribe',$data,array('id'=>$id));
	}else{
		$data['subscriber']=$_SESSION['plugin_name'];
		pdo_insert('core_plugin_subscribe',$data);
	}
	exi('设置成功！','','list');
}
if($_RQ['op']=='del'){
	$dels=array();
	if($_RQ['id']){
		$dels[]=intval($_RQ['id']);
	}
	if($_RQ['dels']){
		$_RQ['dels']=str_array($_RQ['dels']);
		foreach($_RQ['dels'] as $id){
			$dels[]=intval($id);
		}
	}
	foreach($dels as $id){
		pdo_delete('core_plugin_subscribe',array('id'=>$id));
	}
	exi('删除成功！');
}
